%%% TECHNOLOGICAL PROGRESS AND RENT SEEKING ---- MATLAB CODE TO PRODUCE FIGURES 1, 2, 3


clear all



% PARAMETERIZING THE MODEL

gridsize = 2000;

beta = 25;                 			         % Supply of resources per firm.
phi_max = 1;
phi_min = 0;
PHI_a = zeros(gridsize+1,1);
dphi = (phi_max - phi_min)/gridsize;
phi_y_fixed = 0.5;
weight = 1;                                	 % phi_y is allowed to be a weighted avg. of phi_a and phi_y_fixed --- FOR FIGURE 1, SET weight=0 // FOR FIGURE 2, SET weight=1



% CREATING RELEVANT VECTORS

X = zeros(gridsize+1,1);
S = zeros(gridsize+1,1);
MP = zeros(gridsize+1,1);           		% Marginal benefit of surplus creation
MA = zeros(gridsize+1,1);           		% Marginal benefit of surplus appropriation
M_star = zeros(gridsize +1,1);      		% Marginal benefit in equilibrium
F = zeros(gridsize+1,1);            		% Distance to FOC = 0
dx = beta/gridsize;
X_star = zeros(gridsize+1,1);       		% Optimal allocation to surplus appropriation
S_star = zeros(gridsize+1,1);       		% Optimal allocation to surplus creation
O_star = zeros(gridsize+1,1);            	% Avg. firm output with optimal allocation
P_star = zeros(gridsize+1,1);            	% Avg. firm profit with optimal allocation and equilibrium price of resources
O_efficient = zeros(gridsize+1,1);          % Avg. firm output with socially efficient allocation
W_star = zeros(gridsize+1,1);               % Equilibrium price of resources
W_efficient = zeros(gridsize+1,1);          % Price of resources with socially efficient allocation



% NUMERICAL COMPUTATIONS

for m=1:gridsize+1
   PHI_a(m,1) = phi_min + (m-1)*dphi;
   phi_y=weight*PHI_a(m,1) + (1-weight)*phi_y_fixed;
   X_star(m,1) = beta + 100;     		    % Will let me know if something is funky
   f_min = 100;                  		    % High starting value
    
for n=1:gridsize+1
    X(n,1) = (n-1)*dx;
    S(n,1) = beta - X(n,1);
    MP(n,1) = phi_y * (1 - PHI_a(m,1)*(X(n,1)/(1+X(n,1))))/(1 + S(n,1))^2;
    MA(n,1) = phi_y * PHI_a(m,1)*(S(n,1)/(1+S(n,1)))/(1+X(n,1))^2;
    F(n,1) = MP(n,1) - MA(n,1);
    if abs(F(n,1)) < f_min
        f_min = abs(F(n,1));
        X_star(m,1) = X(n,1);
        S_star(m,1) = beta - X(n,1);
        M_star(m,1) = MP(n,1);   
    end
end
O_star(m,1) = phi_y*S_star(m,1)/(1+S_star(m,1)); 
P_star(m,1) = O_star(m,1) - beta *M_star(m,1);
W_star(m,1) = M_star(m,1);
O_efficient(m,1) = phi_y * beta/(1+beta);
W_efficient(m,1) = phi_y /(1+beta)^2;

end




% GENERATING PANELS A, B, C

figure
plot(PHI_a,S_star, PHI_a,X_star,'-.','LineWidth',2);
xlabel('\phi_a')
legend('Surplus creation: s*','Surplus appropriation: x*','location','southeast','FontSize',12 )
ylim([0 beta])

figure
plot(PHI_a, W_efficient, PHI_a,W_star,'-.','LineWidth',2);
xlabel('\phi_a')
legend('Marginal social value of resources: \pi''(b)','Equilibrium price of resources: w*', 'location', 'southeast', 'FontSize',12)
ylim([0 0.0021])

figure
plot(PHI_a, O_efficient, PHI_a,O_star,'-.',PHI_a,P_star, '--','LineWidth',2);
xlabel('\phi_a')
legend('Efficient firm output: \pi(b)','Equilibrium firm output: \pi(s*)', 'Equilibrium firm profit: \pi(s*) - b w*','location', 'southeast', 'FontSize',12)
ylim([0 1])
%xlim([0.75 1])  				        % UNCOMMENT THIS COMMAND TO ZOOM IN AND PRODUCE FIGURE 3


